package com.zys.asseter.service;

import com.zys.asseter.domain.dto.AssetFingerprintTypeDto;
import com.zys.asseter.domain.pojo.AssetFingerprintTypeEntity;
import com.zys.asseter.mapper.AssetFingerprintTypeMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class AssetFingerprintTypeService {

    private final AssetFingerprintTypeMapper assetFingerprintTypeMapper;


    public void insertForManufacturer(AssetFingerprintTypeDto assetFingerprintTypeDto) {
        AssetFingerprintTypeEntity entity= new AssetFingerprintTypeEntity();
        BeanUtils.copyProperties(assetFingerprintTypeDto,entity);
        entity.setId(UUID.randomUUID().toString().replace("-",""));
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setDelFlag("0");
        Integer count=assetFingerprintTypeMapper.countForManufacturer(assetFingerprintTypeDto.getModelName());
        entity.setCount(count);
        entity.setParentModelId("-1");
        String modelId = assetFingerprintTypeMapper.getManuModelId(assetFingerprintTypeDto.getModelName());
        if(modelId==null){
            entity.setModelId(UUID.randomUUID().toString().replace("-",""));
        }else {
            entity.setModelId(modelId);
        }
        assetFingerprintTypeMapper.insert(entity);

    }

    public void insertForType(AssetFingerprintTypeDto assetFingerprintTypeDto) {
        AssetFingerprintTypeEntity entity= new AssetFingerprintTypeEntity();
        BeanUtils.copyProperties(assetFingerprintTypeDto,entity);
        entity.setId(UUID.randomUUID().toString().replace("-",""));
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setDelFlag("0");
        entity.setParentModelId("-1");
        String modelId = assetFingerprintTypeMapper.getTypeModelId(assetFingerprintTypeDto.getModelName());
        entity.setModelId(modelId);
        Integer count = assetFingerprintTypeMapper.countFortype(assetFingerprintTypeDto.getModelName());
        entity.setCount(count);
        assetFingerprintTypeMapper.insert(entity);
    }

    public void insertSubType(AssetFingerprintTypeDto assetFingerprintTypeDto) {
        AssetFingerprintTypeEntity entity= new AssetFingerprintTypeEntity();
        BeanUtils.copyProperties(assetFingerprintTypeDto,entity);
        entity.setId(UUID.randomUUID().toString().replace("-",""));
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setDelFlag("0");
        String modelId = assetFingerprintTypeMapper.getSubTypeModelId(assetFingerprintTypeDto.getParentModelId());
        entity.setModelId(modelId);
        Integer count = assetFingerprintTypeMapper.countForSubtype(assetFingerprintTypeDto.getParentModelId());
        entity.setCount(count);
        assetFingerprintTypeMapper.insert(entity);
    }
}
